<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>prefix 查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-prefix-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-prefix-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-prefix-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-prefix-query.html" rel="nofollow" target="_blank">../en/query-dsl-prefix-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="term-level-queries.html">词级查询</a></span>
»
<span class="breadcrumb-node">prefix 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-dsl-ids-query.html">« ids</a>
</span>
<span class="next">
<a href="query-dsl-range-query.html">range 查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-prefix-query"></a>前缀查询 (prefix query)
</h2>
</div></div></div>

<p>返回在指定的字段中包含特定前缀的文档。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="prefix-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<p>
下面这个搜索返回 <code class="literal">user</code> 字段包含以 <code class="word">ki</code> 开头的词项的文档。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "prefix": {
            "user": {
                "value": "ki"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/280.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="prefix-query-top-level-params"></a><code class="literal">prefix</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">&lt;field&gt;</code>
</span>
</dt>
<dd>
(必需, object) 你想要搜索的字段。
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="prefix-query-field-params"></a><code class="literal">&lt;field&gt;</code>的参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">value</code>
</span>
</dt>
<dd>
(必需, string) 你希望在指定的 <code class="literal">&lt;field&gt;</code> 中查找的词项的起始字符。
</dd>
<dt>
<span class="term">
<code class="literal">rewrite</code>
</span>
</dt>
<dd>
(可选, string) 用于重写查询的方法。有效值及更多信息请参考 <a class="xref" href="query-dsl-multi-term-rewrite.html" title="rewrite 参数" rel="nofollow"><code class="literal">rewrite</code> 参数</a>。
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="prefix-query-notes"></a>注意
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="prefix-query-short-ex"></a>短请求格式的示例
</h4>
</div></div></div>
<p>
可以通过组合 <code class="literal">&lt;field&gt;</code> 和 <code class="literal">value</code> 参数来简化 <code class="literal">prefix</code> 查询语法。例如:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "query": {
        "prefix" : { "user" : "ki" }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/281.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="prefix-query-index-prefixes"></a>提高前缀查询的速度
</h4>
</div></div></div>
<p>
可以使用 <a class="xref" href="index-prefixes.html" title="index_prefixes" rel="nofollow"><code class="literal">index_prefixes</code></a> 映射参数来提高前缀查询的速度。

如果启用了该参数，Elasticsearch 会在单独的字段中索引 2 到 5 个字符的前缀。

这让 Elasticsearch 以更大的索引为代价去更有效地运行前缀查询。
</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="prefix-query-allow-expensive-queries"></a>允许执行昂贵的查询
</h4>
</div></div></div>
<p>
如果 <a class="xref" href="query-dsl.html#query-dsl-allow-expensive-queries"><code class="literal">search.allow_expensive_queries</code></a> 设置为<code class="literal">false</code>，则前缀查询不会被执行。

即使有此设置，但是如果启用了 <a class="xref" href="index-prefixes.html" title="index_prefixes"><code class="literal">index_prefixes</code></a>，则会构建一个优化的查询，该查询不会被认为很慢，因此会被执行。
</p>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="query-dsl-ids-query.html">« ids</a>
</span>
<span class="next">
<a href="query-dsl-range-query.html">range 查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>